package GUILayer;
import CtrLayer.*;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JMenuItem;

public class RegistreItemGUI extends JFrame {

	private JFrame frmRegistrerVare;
	private JTextField stregkodeField;
	private JTextField vareNavnField;
	private JTextField salgsprisField;
	private JTextField indkoebsprisField;
	private JTextField lagerPlaceringField;
	private JTextField discountField;
	private JTextField antalField;
	private JTextField minimumBeholdningField;
	private JTextField opfyldningsField;
	private ItemCtr itemCtr;
	private JTextField vareBeskrivelsesField;
	private static final long serialVersionUID = 7526472295622776147L;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegistreItemGUI window = new RegistreItemGUI();
					window.frmRegistrerVare.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public RegistreItemGUI() {
		initialize();
		itemCtr = itemCtr.getInstance();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRegistrerVare = new JFrame();
		frmRegistrerVare.setTitle("Registrer vare");
		frmRegistrerVare.setBounds(100, 100, 633, 355);
		frmRegistrerVare.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmRegistrerVare.setJMenuBar(menuBar);
		
		JMenu mnFiler = new JMenu("Filer");
		menuBar.add(mnFiler);
		
		JMenuItem mntmAfslutProgram = new JMenuItem("Afslut program");
		mntmAfslutProgram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnFiler.add(mntmAfslutProgram);
		
		JMenu mnHjlp = new JMenu("Hj\u00E6lp");
		menuBar.add(mnHjlp);
		
		JMenuItem om = new JMenuItem("Om");
		om.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAbout();
			}
		});
		mnHjlp.add(om);
		
		JLabel lblNewLabel = new JLabel("Stregkode:");
		lblNewLabel.setBounds(10, 17, 99, 14);
		
		JLabel lblNewLabel_1 = new JLabel("Vare navn:");
		lblNewLabel_1.setBounds(10, 45, 99, 14);
		
		JLabel lblNewLabel_3 = new JLabel("Salgspris:");
		lblNewLabel_3.setBounds(10, 70, 99, 14);
		
		JLabel lblNewLabel_4 = new JLabel("Indk\u00F8bspris:");
		lblNewLabel_4.setBounds(10, 95, 99, 14);
		
		JLabel lblNewLabel_5 = new JLabel("Lager placering:");
		lblNewLabel_5.setBounds(10, 127, 99, 14);
		
		JLabel lblNewLabel_6 = new JLabel("Discount:");
		lblNewLabel_6.setBounds(10, 152, 99, 14);
		
		JLabel lblNewLabel_7 = new JLabel("Minimum beholdning:");
		lblNewLabel_7.setBounds(10, 204, 128, 14);
		
		JLabel lblNewLabel_8 = new JLabel("Opfyldnings antal:");
		lblNewLabel_8.setBounds(10, 230, 128, 14);
		
		JLabel lblNewLabel_9 = new JLabel("Antal:");
		lblNewLabel_9.setBounds(10, 178, 99, 14);
		
		stregkodeField = new JTextField();
		stregkodeField.setBounds(148, 14, 130, 20);
		stregkodeField.setColumns(10);
		
		vareNavnField = new JTextField();
		vareNavnField.setBounds(148, 40, 130, 20);
		vareNavnField.setColumns(10);
		
		salgsprisField = new JTextField();
		salgsprisField.setBounds(148, 66, 86, 20);
		salgsprisField.setColumns(10);
		
		indkoebsprisField = new JTextField();
		indkoebsprisField.setBounds(148, 92, 86, 20);
		indkoebsprisField.setColumns(10);
		
		lagerPlaceringField = new JTextField();
		lagerPlaceringField.setBounds(148, 120, 86, 20);
		lagerPlaceringField.setColumns(10);
		
		discountField = new JTextField();
		discountField.setBounds(148, 146, 86, 20);
		discountField.setColumns(10);
		
		antalField = new JTextField();
		antalField.setBounds(148, 172, 86, 20);
		antalField.setColumns(10);
		
		minimumBeholdningField = new JTextField();
		minimumBeholdningField.setBounds(148, 198, 86, 20);
		minimumBeholdningField.setColumns(10);
		
		opfyldningsField = new JTextField();
		opfyldningsField.setBounds(148, 224, 86, 20);
		opfyldningsField.setColumns(10);
		
		JButton btnNewButton = new JButton("Registrer vare");
		btnNewButton.setBounds(10, 262, 128, 23);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
				registreItem();
				}
				catch(Exception registrerVareError)
				{
					JOptionPane.showMessageDialog(frmRegistrerVare, "Udfyld alle felter eller \n" + "tjek om felterne er udfyldt korrekt", "Error Message", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		JButton btnNewButton_1 = new JButton("Ryd");
		btnNewButton_1.setBounds(148, 262, 60, 23);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearFields();
			}
		});
		
		JButton btnNewButton_2 = new JButton("Tilbage");
		btnNewButton_2.setBounds(218, 262, 86, 23);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new ItemGUI();
				frmRegistrerVare.dispose();
				
			}
		});
		
		JLabel lblKr = new JLabel("Kr.");
		lblKr.setBounds(238, 69, 53, 14);
		
		JLabel lblKr_1 = new JLabel("Kr.");
		lblKr_1.setBounds(238, 95, 53, 14);
		
		JLabel label_4 = new JLabel("%");
		label_4.setBounds(238, 149, 53, 14);
		
		vareBeskrivelsesField = new JTextField();
		vareBeskrivelsesField.setBounds(346, 34, 154, 155);
		vareBeskrivelsesField.setColumns(10);
		
		JLabel lblVareBesrkivelse = new JLabel("Vare beskrivelse");
		lblVareBesrkivelse.setBounds(346, 14, 154, 14);
		frmRegistrerVare.getContentPane().setLayout(null);
		frmRegistrerVare.getContentPane().add(lblNewLabel);
		frmRegistrerVare.getContentPane().add(lblNewLabel_1);
		frmRegistrerVare.getContentPane().add(lblNewLabel_3);
		frmRegistrerVare.getContentPane().add(lblNewLabel_4);
		frmRegistrerVare.getContentPane().add(lblNewLabel_5);
		frmRegistrerVare.getContentPane().add(lblNewLabel_6);
		frmRegistrerVare.getContentPane().add(lblNewLabel_9);
		frmRegistrerVare.getContentPane().add(lblNewLabel_7);
		frmRegistrerVare.getContentPane().add(stregkodeField);
		frmRegistrerVare.getContentPane().add(vareNavnField);
		frmRegistrerVare.getContentPane().add(discountField);
		frmRegistrerVare.getContentPane().add(opfyldningsField);
		frmRegistrerVare.getContentPane().add(minimumBeholdningField);
		frmRegistrerVare.getContentPane().add(antalField);
		frmRegistrerVare.getContentPane().add(lagerPlaceringField);
		frmRegistrerVare.getContentPane().add(indkoebsprisField);
		frmRegistrerVare.getContentPane().add(salgsprisField);
		frmRegistrerVare.getContentPane().add(lblKr_1);
		frmRegistrerVare.getContentPane().add(lblKr);
		frmRegistrerVare.getContentPane().add(label_4);
		frmRegistrerVare.getContentPane().add(vareBeskrivelsesField);
		frmRegistrerVare.getContentPane().add(lblVareBesrkivelse);
		frmRegistrerVare.getContentPane().add(btnNewButton_1);
		frmRegistrerVare.getContentPane().add(btnNewButton_2);
		frmRegistrerVare.getContentPane().add(btnNewButton);
		frmRegistrerVare.getContentPane().add(lblNewLabel_8);
		frmRegistrerVare.setVisible(true);
	}
	
	private void registreItem()
	{
		String barCode = stregkodeField.getText();
		
		String itemName = vareNavnField.getText();
		
		String description = vareBeskrivelsesField.getText();
		
		double retailPrice = Double.parseDouble(salgsprisField.getText());
		
		double buyingPrice = Double.parseDouble(indkoebsprisField.getText());
		
		String location = lagerPlaceringField.getText();
		
		double discount = Double.parseDouble(discountField.getText());
		
		int amount = Integer.parseInt(antalField.getText());
		
		int minAmount = Integer.parseInt(minimumBeholdningField.getText());
		
		int restockAmount = Integer.parseInt(opfyldningsField.getText());
		
		itemCtr.createItem(barCode, itemName, description, retailPrice, buyingPrice, location, discount, minAmount, restockAmount, amount);
		clearFields();
	}


private void clearFields()
{
	stregkodeField.setText("");
	
	vareNavnField.setText("");
	
	salgsprisField.setText("");
	
	indkoebsprisField.setText("");
	
	lagerPlaceringField.setText("");
	
	discountField.setText("");
	
	antalField.setText("");
	
	minimumBeholdningField.setText("");
	
	opfyldningsField.setText("");
	
	vareBeskrivelsesField.setText("");
}

private void showAbout()
{
	JOptionPane.showMessageDialog(frmRegistrerVare, "Vestbjerg Byggecenter\n" + "Version 1.0", "About Vestbjerg Byggecenter", JOptionPane.INFORMATION_MESSAGE);
}
}
